package ar.com.hakasoft

class FacturasSinCobrarController extends LoginController {
	
	def facturasService
	
	def beforeInterceptor = [action:this.&checkUser,except:[]]

    def index = { 
		def listaFacturasSinCobrar = facturasService.obtenerFacturasSinCobrar()
		
		[listaFacturasSinCobrar: listaFacturasSinCobrar]
	}
	
	def cobrar = {
		println params
		def idFactura = params.idFactura
		if (idFactura){
			def factura = facturasService.obtenerFactura(idFactura, session?.user?.username)
			def cobroParcial = params."cobroParcial$idFactura"
			if (cobroParcial) {
				cobroParcial = new BigDecimal (cobroParcial)
				if (cobroParcial < factura.precioTotal){
					factura.sumarPrecioParcial(cobroParcial)
				} 
			}else{
				factura.cobrar(true)
			}
			facturasService.guardarFactura(factura)
		}
		redirect(action: "index")
	}
	
	def eliminarFactura = {
		facturasService.eliminarFactura(params.id)
		redirect(action: "index")
	}
}
